﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class OptionsState : State
    {
        private const float TITLE_CHOICE_SEPERATION = 220f;
        private const float CHOICE_SEPERATION = 50f;
        public SpriteFont font;
        public SpriteFont titleFont;

        // Bool shift titles down only once
        bool keyHeld = false;

        public OptionsState(SpriteFont newFont, SpriteFont newTitleFont, GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
            //background = newBackground;
            font = newFont;
            titleFont = newTitleFont;

            // Setup choices.
            choices.Add("Controls");
            choices.Add("Display");
            selectedChoice = 0;
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            base.onEnter(state);
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            
            int i;

            spriteBatch.Begin();

                // Display the background.
                //spriteBatch.Draw(background, screenSize, Color.White);
                spriteBatch.DrawString(titleFont, "OPTIONS", new Vector2(screenSize.X / 2, 0), Color.White);

                // Display the choices.
                for (i = 0; i < choices.Count(); i++)
                {
                    if (i == selectedChoice) // Is the current choice being printed.
                        spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, TITLE_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Red);
                    else // The current choice is not being printed.
                        spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, TITLE_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Yellow);
                }
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
            foreach (Keys key in keys)
            {
                if ((key == Keys.Up || dpad.Up == ButtonState.Pressed) && !keyHeld) // The up button was pressed.
                {
                    selectedChoice = (selectedChoice - 1) % choices.Count();

                    if (selectedChoice < 0)
                        selectedChoice = choices.Count() - 1;

                    // Key has been pressed
                    keyHeld = true;

                }
                else if ((key == Keys.Down || dpad.Down == ButtonState.Pressed) && !keyHeld) // The down button was pressed.
                {
                    selectedChoice = (selectedChoice + 1) % choices.Count();

                    // Key has been pressed
                    keyHeld = true;
                }
            }

            // Reset key press state
            if (Keyboard.GetState().IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Down))
            {
                keyHeld = false;
            }

            base.input(gameTime, keys, buttons, dpad, thumbSticks);
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            base.update(gameTime);
        }
    }
}
